/**
 *	流程模块接口文件：出入库操作
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2023-07-19
 */

package com.framework.Module.jxc.jxccrku;




import com.framerock.baselib.CDate;

import com.framerock.baselib.Rock;
import com.framework.ACore.Model;
import com.framework.Flow.FlowListener;
import com.framework.Model.ModelOption;

import java.util.ArrayList;
import java.util.Map;


public class FlowListenerjxccrku extends FlowListener {



    @Override
    public Boolean onModejs() {
        return true;
    }

    @Override
    public ArrayList<Map<String, String>> onStore(String action, String params) {
        if(action.equals("kinddata"))return kinddata();
        if(action.equals("ckiddata"))return ckiddata();
        if(action.equals("custdata"))return custdata();
        return null;
    }

    //出入库类型
    public ArrayList<Map<String, String>> kinddata()
    {
        ArrayList<Map<String, String>> rows = Rock.getMapList(),data;
        ModelOption option = ModelOption.get(this.nDb);
        rows.add(Rock.getMap("optgroup=start,name=入库"));
        data = option.getData("jxcstockin", mFlow.COMPANYID, "value");
        if(data!=null)for(int i=0;i<data.size();i++){
            rows.add(data.get(i));
        }
        rows.add(Rock.getMap("optgroup=end"));

        rows.add(Rock.getMap("optgroup=start,name=出库"));
        data = option.getData("jxcstockout", mFlow.COMPANYID, "value");
        if(data!=null)for(int i=0;i<data.size();i++){
            rows.add(data.get(i));
        }
        rows.add(Rock.getMap("optgroup=end"));
        return rows;
    }


    //读取仓库
    public ArrayList<Map<String, String>> ckiddata()
    {
        ArrayList<Map<String, String>> rows;
        rows = Model.get(this.nDb, "jxcstock").getAll("`cid`="+mFlow.COMPANYID+"", "`id` AS value,`name`", "`sort` ASC");
        return rows;
    }

    //读取客户
    public ArrayList<Map<String, String>> custdata()
    {
        ArrayList<Map<String, String>> rows = Rock.getMapList();
        return rows;
    }

    @Override
    public Map<String, String> onSavebefore(Map<String, String> da, String mid, Boolean isadd) {
        int kind = Rock.getMapint(da, "kind");
        Map<String, String> map = Rock.getMap();
        String type = "0";
        if(kind>=20)type="1";//入库
        map.put("savefields", "type="+type+"");
        return map;
    }

    @Override
    public Map<String, String> onoptMenurunBefore(Map<String, String> cdrs) {
        //出入库操作保存到jxcgoods表
        if(cdrs.get("num").equals("crku")){
            Map<String, String> da,sda = Rock.getMap();
            String dt = mRequest.post("dt", CDate.date());
            sda.put("dt", dt);
            sda.put("type", mFlow.getFieldsval("type"));
            sda.put("kind", mFlow.getFieldsval("kind"));
            sda.put("ckid", mRequest.post("ckid"));
            sda.put("custname", mFlow.getFieldsval("custname"));
            sda.put("custid", mFlow.getFieldsval("custid"));
            sda.put("cid", mFlow.getFieldsval("cid"));
            sda.put("aid", mRequest.USEAID);
            sda.put("optname", mRequest.getUserars("name"));
            sda.put("optdt", CDate.now());
            sda.put("mid", mFlow.mid);
            int type  = Integer.parseInt(mFlow.getFieldsval("type"));
            Model obj = Model.get(this.nDb,"jxcgoods");
            ArrayList<Map<String, String>> rows = Model.get(this.nDb,"jxcgoodn").getAll("`mid`="+mFlow.mid+"", "*", "`sort` ASC");
            String sids = "0";
            if(rows != null)for(int i=0;i<rows.size();i++){
                da = rows.get(i);
                double count = Double.parseDouble(da.get("count"));
                if(count<0)count = 0 - count;
                if(type==1)count = 0 - count; //出库要为负数
                sda.put("sname", da.get("sname"));
                sda.put("sid", da.get("sid"));
                sda.put("count", ""+count);
                sda.put("sunit", da.get("sunit"));
                sda.put("price", da.get("price"));
                sda.put("explain", da.get("remark"));
                sda.put("sfile", da.get("sfile"));
                obj.insertbo(sda);
                sids += ","+da.get("sid")+"";
            }

            da = Rock.getMap();
            da.put("state", "1");
            da.put("inoutname", mRequest.getUserars("name"));
            da.put("inoutid", mRequest.USEAID);
            da.put("inouttime", CDate.now());
            mFlow.mModel.update(da, mFlow.mid);
            mFlow.let("jxcbase").action("reloadstock", sids);//更新库存
        }
        return super.onoptMenurunBefore(cdrs);
    }
}
